<?php
include_once('../../app/connection.php');

if(isset($_POST['action']))
{
    $action = $_POST['action'];
    
    if($action == 'delete')
    {
        if(isset($_POST['mamh']))
        {
            $mamh = $_POST['mamh'];
            
            $str = "SELECT * FROM monhoc WHERE MAMONHOC='".$mamh."' AND TRANGTHAI=1";
            $check = mysql_query($str);
            if(mysql_num_rows($check) == 0)
                echo 'Môn học '. $mamh .' không tồn tại'; 
            else {
                $sql = "UPDATE monhoc SET TRANGTHAI=0 WHERE MAMONHOC='".$mamh."'";
                $result = mysql_query($sql);       
                echo 'Đã xóa môn học ' . $mamh;
            }
        }
        
        if(isset($_POST['array']))
        {
            $array = $_POST['array'];
                        
            for($i=0; $i < count($array); $i++)
            {
                $sql = "UPDATE monhoc SET TRANGTHAI=0 WHERE MAMONHOC='".$array[$i]."'";
                $result = mysql_query($sql);
            }
            
        }
    }
    
    if($action == 'suggest')
    {
        if(isset($_POST['mamh']))
        {
            $mamh = $_POST['mamh'];            
            $sql = "SELECT * FROM monhoc WHERE MAMONHOC LIKE '". $mamh . "%' AND TRANGTHAI=1";
            $result = mysql_query($sql);
            
            if(mysql_num_rows($result) == 0)
                echo 'Môn học '. $mamh .' không tồn tại';
            else                
                while($row = mysql_fetch_assoc($result))
                {
                    echo $row['MAMONHOC'].' - '.$row['TENMONHOC'].'<br />';
                }
        }
    }
    if($action == 'load_list')
    {
    ?>
        <center><table>
            <tr style="background: #247CB9;">
                <td style="width: 100px; text-align: center; color: #fff;">Mã MH</td>
                <td style="width: 200px; text-align: center; color: #fff;">Tên MH</td>
                <td style="width: 50px; text-align: center; color: #fff;">TC Lý thuyết</td>
                <td style="width: 50px; text-align: center; color: #fff;">TC Thực hành</td>
                <td style="width: 50px; text-align: center; color: #fff;">Khoa</td>
                <td style="width: 50px; text-align: center; color: #fff;">Chọn</td>
            </tr>
    <?php
            $sql = mysql_query("SELECT * FROM monhoc WHERE TRANGTHAI=1");
            while($rows = mysql_fetch_assoc($sql))
            {
    ?>
                 <tr>
                 <td style="text-align: center;"><?php echo $rows['MAMONHOC']; ?></td>
                 <td><?php echo $rows['TENMONHOC']; ?></td>
                 <td style="text-align: center;"><?php echo $rows['TINCHILYTHUYET']; ?></td>
                 <td style="text-align: center;"><?php echo $rows['TINCHITHUCHANH']; ?></td>
                 <td style="text-align: center;"><?php echo $rows['MAKHOA']; ?></td>
                 <td style="text-align: center;"><input type="checkbox" name="chk_mh[]" id="chk_mh" value="<?php echo $rows['MAMONHOC']; ?>" /></td>
                 </tr>
        <?php } ?>
        </table></center>
    <?php
    }
}

?>